package com.calf.module.message.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.calf.cn.aspect.MethodLog;
import com.calf.cn.controller.BaseController;
import com.calf.cn.entity.DataTables;
import com.calf.cn.service.BaseManager;
import com.calf.cn.utils.DateUtil;
import com.calf.cn.utils.SearchUtil;
import com.calf.cn.utils.UUIDUtils;
import com.calf.module.message.entity.Message;
import com.calf.module.message.entity.MessageConstant;

@Controller
@RequestMapping(value="/message/send")
public class SendMessageController implements BaseController<Message> {

	@Autowired
	private BaseManager baseManager;
	
	@Override
	public String home() {
		return "message/sendmessage" ;
	}

	@Override
	@SuppressWarnings("unchecked")
	public DataTables<Message> initTable(HttpServletRequest request) {
		Map<String,Object> parameters = SearchUtil.convertorEntitysToMap(request.getParameterMap());
		String sEcho = (String)parameters.get("sEcho");
		//解析查询条件
		String createMan = (String)parameters.get("createMan");
		String startTime = (String)parameters.get("startTime");
		String endTime = (String)parameters.get("endTime");
		
		List<Message> aaData = baseManager.query(Message.class, parameters);
		//组装查询count参数
		parameters.put("startTime", startTime);
		parameters.put("endTime", endTime);
		parameters.put("createMan", createMan);
		
		Integer iTotalDisplayRecords = baseManager.get("Message.queryCount", parameters);
		
		return new DataTables<Message>(sEcho, aaData, aaData.size(), iTotalDisplayRecords);
	}
	
	
	@Override
	@RequiresPermissions(value={"message:add:add"})
	public String addAndUpdateHome(Model model, String id) {
		return "/message/addmessage";
	}

	@Override
	@MethodLog(operType="新增")
	@RequiresPermissions(value="message:add:add")
	public int saveAdd(Message entity) {
		//获取操作者帐号
		Subject currentUser = SecurityUtils.getSubject();
		Message message = new Message() ;
		message.setMessageId(UUIDUtils.getUUID());
		message.setCreateMan(String.valueOf(currentUser.getPrincipal()));
		message.setCreateTime(DateUtil.dateFormat());
		message.setModifyMan(String.valueOf(currentUser.getPrincipal()));
		message.setModifyTime(DateUtil.dateFormat());
		message.setContent(entity.getContent());
		message.setTitle(entity.getTitle());
		message.setPhoneNum(entity.getPhoneNum());
		message.setIsRead(MessageConstant.Isread.unRead);
		message.setMessageType(entity.getMessageType());
		message.setState(MessageConstant.state.active);
		message.setMessageId(MessageConstant.generateId());
	//	if (entity.getSendType() == 1) {
		//	message.setCellPhone(entity.getCellPhone());
		//} else {
		//	message.setCellPhone(null);
		//}
		
		return baseManager.insert(message) ;
	}

	@Override
	@MethodLog(operType="删除")
	@RequiresPermissions(value="message:delete")
	public int delete(Long id) {
		return 0;
	}

	@Override
	public int saveUpdate(Message entity) {
		return 0;
	}

	/* (非 Javadoc)
	 * Description:
	 * @see com.calf.cn.controller.BaseController#delete(java.lang.String)
	 */
	
	@Override
	public int delete(String id) {
		return 0;
	}

}
